﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using GlobalHelper;
using IBSGate;
using Standardization;
using OPMSIBSDBGate;

namespace Loader
{
    public class CreateTimeTable
    {
        private readonly DataTable _dt;
        //private readonly string _lineID;
        private string _mainId = string.Empty;

        public CreateTimeTable(DataTable dt)
        {
            _dt = dt;
        }

        #region Create MainInfo

        

        public string CreateMainInfoWithoutLineIdWithSationName()
        {
            string result = string.Empty;

            #region Get Base Info

            string mainInfo = string.Empty;


            int colAmount = _dt.Columns.Count;
            int rowAmount = _dt.Rows.Count;

            //set info
            for (int i = 0; i < colAmount; i++)
            {
                if (!string.IsNullOrEmpty(_dt.Rows[0][i].ToString().Trim()))
                {
                    mainInfo = _dt.Rows[0][i].ToString().Trim();
                    break;
                }
            }

            mainInfo = mainInfo.Replace(" ", "");

            if (mainInfo.Length > 50)
            {
                mainInfo = mainInfo.Remove(50, mainInfo.Length - 1);
            }

            #endregion

            #region Get Station Name

            string beginStationName = string.Empty;
            string endStationName = string.Empty;

            int beginColNum = 0;
            int beginRowNum = 0;

            //set info
            for (int i = 0; i < colAmount; i++)
            {
                for (int j = 0; j < rowAmount; j++)
                {
                    if (!string.IsNullOrEmpty(_dt.Rows[j][i].ToString().Trim()) && _dt.Rows[j][i].ToString().Trim() == Helper.TokenLineBeginFlag)
                    {
                        beginColNum = i;
                        beginRowNum = j;
                        break;
                    }    
                }
            }

            if (beginColNum + 2 < colAmount)
            {
                beginStationName = _dt.Rows[beginRowNum][beginColNum + 1].ToString().Trim();
                endStationName = _dt.Rows[beginRowNum][beginColNum + 2].ToString().Trim();
            }

            if (String.IsNullOrEmpty(beginStationName) || string.IsNullOrEmpty(endStationName))
            {
                throw new Exception("起讫站名获取失败!");
                return null;
            }

            #endregion

            string busScheduleMainInfo = mainInfo;

            result = CreateMainRecord(busScheduleMainInfo,beginStationName,endStationName);

            _mainId = result;

            return result;
        }

        
        /// <summary>
        ///  生成主信息
        /// 自动获取
        /// 1. 启用日期
        /// 2. 使用类别UserSort
        /// 3. 时刻表名称
        /// 4. 线路ID
        /// </summary>
        /// <param name="busScheduleMainInfo"></param>
        /// <param name="beginStation"></param>
        /// <param name="endStation"></param>
        /// <returns></returns>
        private string CreateMainRecord(string busScheduleMainInfo,string beginStation,string endStation)
        {
            if (string.IsNullOrEmpty(busScheduleMainInfo))
            {
                throw new Exception("输入的时刻表标题为空!");
            }

            string result = string.Empty;

            #region Get Info

            string lineName = MainInfoOperator.GetLineName(busScheduleMainInfo);
            string lineId = ITDCGate.DBGate.GetLineId(lineName);

            string name = MainInfoOperator.GetBusScheduleName(busScheduleMainInfo);
            string useSort = MainInfoOperator.GetUseSort(busScheduleMainInfo);

            DateTime begin = Helper.BeginDate;
            DateTime end = Helper.EndDate;

            string level = Convert.ToInt32(GlobalHelper.Level.Common).ToString();
            bool valid = true;

            #endregion

            try
            {
                OPMSIBSDBGate.DBGate dbGate = new OPMSIBSDBGate.DBGate();
                result = dbGate.CreateBusScheduleMainInfo(Convert.ToInt32(lineId), Convert.ToInt32(lineId.ToString()),
                                                          begin, end, name, valid, Convert.ToInt32(level), useSort,
                                                          beginStation,endStation
                    );
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

        #endregion

        #region Create DetailInfo

       

        public void CreateDetailInfo(Guid mainID,BusCompany busCompanyFlag)
        {
            if (string.IsNullOrEmpty(mainID.ToString()))
            {
                return;
            }

            if (Helper.IsEmpty(_dt))
            {
                return;
            }

            DataTable operatorDt = _dt;

            int tokenAmount;
            int turnAmount;
            int showRowAmount;
            int showColAmount;

            #region Pre Operator

            //1. 去除 标题行 与 站名行
            operatorDt.Rows[0].Delete();
            operatorDt.Rows[1].Delete();
            operatorDt.AcceptChanges();

            //2. 路牌名 规整 (最好先做)
            //operatorDt = PreOperator.SetTokenName(operatorDt);

            //3. 获取 路牌数 与 周转数 , 显示时的行数
            tokenAmount = PreOperator.GetTokenAmount(operatorDt);
            turnAmount = PreOperator.GetTurnAmount(operatorDt);
            showRowAmount = PreOperator.GetShowRowAmount(operatorDt);
            showColAmount = PreOperator.GetShowColAmount(operatorDt);


            //4. 移除特殊符号 (为应对巴士集团所有线路的情况)
            //operatorDt = PreOperator.RemoveFlag(operatorDt);

            //5. 合并路牌结束符号
            //operatorDt = PreOperator.FixFlag(operatorDt);

            //规整 时刻表
            var standardFactory = new StandardizationFactory();
            IStandardIzation standardIzation = standardFactory.GetStandardization(busCompanyFlag);
            operatorDt = standardIzation.StandardTimeTable(operatorDt);

            #endregion

            #region Update MainInfo

            OPMSIBSDBGate.DBGate dbGate = new OPMSIBSDBGate.DBGate();
            dbGate.UpdateBusScheduleMainInfoWithTokenAndTurnAmount(mainID, tokenAmount, turnAmount, showRowAmount,showColAmount);

            #endregion

            #region Create Detail

            int rowNum;
            int colNum;
            string content;

            for (int i = 0; i < operatorDt.Rows.Count; i++)
            {
                for (int j = 0; j < operatorDt.Columns.Count; j++)
                {
                    if (!string.IsNullOrEmpty(operatorDt.Rows[i][j].ToString()))
                    {
                        rowNum = i;
                        colNum = j;
                        content = operatorDt.Rows[i][j].ToString();


                        try
                        {
                            OPMSIBSDBGate.DBGate dbGateDetail = new OPMSIBSDBGate.DBGate();
                            dbGateDetail.CreateBusScheduleDetailInfo(mainID, rowNum, colNum, content);
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }

                }
            }

            #endregion

        }

        #endregion


        #region NoUse

        //private void CreateDetailInfo()
        //{
        //    if (string.IsNullOrEmpty(_mainId))
        //    {
        //        throw new Exception("主记录不存在.");
        //    }

        //    CreateDetailInfo(_mainId);
        //}

        ///// <summary>
        /////  生成主信息
        ///// 自动获取
        ///// 1. 启用日期
        ///// 2. 使用类别UserSort
        ///// 3. 时刻表名称
        ///// 4. 线路ID
        ///// </summary>
        ///// <param name="busScheduleMainInfo"></param>
        ///// <returns></returns>
        //private string CreateMainRecord(string busScheduleMainInfo)
        //{
        //    if (string.IsNullOrEmpty(busScheduleMainInfo))
        //    {
        //        throw new Exception("输入的时刻表标题为空!");
        //    }

        //    string result = string.Empty;

        //    #region Get Info

        //    string lineName = MainInfoOperator.GetLineName(busScheduleMainInfo);
        //    string lineId = ITDCGate.DBGate.GetLineId(lineName);

        //    string name = MainInfoOperator.GetBusScheduleName(busScheduleMainInfo);
        //    string useSort = MainInfoOperator.GetUseSort(busScheduleMainInfo);

        //    DateTime begin = Helper.BeginDate;
        //    DateTime end = Helper.EndDate;

        //    string level = Convert.ToInt32(GlobalHelper.Level.Common).ToString();
        //    bool valid = true;

        //    #endregion

        //    try
        //    {
        //        IBSGate.DBGate dbGate = new IBSGate.DBGate();
        //        result = dbGate.CreateBusScheduleMainInfo(Convert.ToInt32(lineId), Convert.ToInt32(lineId.ToString()),
        //                                                  begin, end, name, valid, Convert.ToInt32(level), useSort
        //            );
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }

        //    return result;
        //}


        //private string CreateMainInfoWithoutLineId()
        //{
        //    string result = string.Empty;

        //    #region Get Base Info

        //    string mainInfo = string.Empty;


        //    int colAmount = _dt.Columns.Count;

        //    //set info
        //    for (int i = 0; i < colAmount; i++)
        //    {
        //        if (!string.IsNullOrEmpty(_dt.Rows[0][i].ToString().Trim()))
        //        {
        //            mainInfo = _dt.Rows[0][i].ToString().Trim();
        //            break;
        //        }
        //    }

        //    #endregion

        //    if (mainInfo.Length > 50)
        //    {
        //        mainInfo = mainInfo.Remove(50, mainInfo.Length - 1);
        //    }

        //    string busScheduleMainInfo = mainInfo;

        //    result = CreateMainRecord(busScheduleMainInfo);

        //    _mainId = result;

        //    return result;
        //}

        //public CreateTimeTable(DataTable dt, string lineId)
        //{
        //    _dt = dt;
        //    _lineID = lineId;
        //}

        //public string CreateMainInfo()
        //{
        //    string result = string.Empty;

        //    #region Get Base Info

        //    string mainInfo = string.Empty;


        //    int colAmount = _dt.Columns.Count;

        //    //set info
        //    for (int i = 0; i < colAmount; i++)
        //    {
        //        if (!string.IsNullOrEmpty(_dt.Rows[0][i].ToString()))
        //        {
        //            mainInfo = _dt.Rows[0][i].ToString();
        //            break;
        //        }
        //    }

        //    #endregion

        //    string busScheduleMainInfo = mainInfo;

        //    result = CreateMainRecord(busScheduleMainInfo, _lineID.ToString());

        //    return result;
        //}

        ///// <summary>
        ///// 生成主信息
        ///// 自动获取
        ///// 1. 启用日期
        ///// 2. 使用类别UserSort
        ///// 3. 时刻表名称
        ///// </summary>
        ///// <param name="busScheduleMainInfo"></param>
        ///// <param name="lineID"></param>
        ///// <returns>mainId</returns>
        //private string CreateMainRecord(string busScheduleMainInfo, string lineID)
        //{
        //    string result = string.Empty;

        //    //get info
        //    string name = MainInfoOperator.GetBusScheduleName(busScheduleMainInfo);
        //    string useSort = MainInfoOperator.GetUseSort(busScheduleMainInfo);
        //    string ibdmId = MainInfoOperator.GetIbdmId(lineID);
        //    DateTime begin = Helper.BeginDate;
        //    DateTime end = Helper.EndDate;
        //    string level = Helper.Level;
        //    bool valid = true;

        //    if (!string.IsNullOrEmpty(busScheduleMainInfo))
        //    {
        //        IBSGate.DBGate dbGate = new IBSGate.DBGate();
        //        result = dbGate.CreateBusScheduleMainInfo(Convert.ToInt32(lineID), Convert.ToInt32(ibdmId),
        //                                         begin, end, name, valid, Convert.ToInt32(level), useSort
        //            );
        //    }

        //    return result;
        //}

        #endregion
    }
}
